package cn.shiwei.pet.service.impl;

import cn.shiwei.basic.exception.MyException;
import cn.shiwei.basic.service.impl.BaseServiceImpl;
import cn.shiwei.basic.util.DistanceUtil;
import cn.shiwei.basic.util.PageList;
import cn.shiwei.basic.util.Point;
import cn.shiwei.org.domain.Shop;
import cn.shiwei.org.mapper.ShopMapper;
import cn.shiwei.pet.domain.Pet;
import cn.shiwei.pet.domain.PetDetail;
import cn.shiwei.pet.domain.SearchMasterMsg;
import cn.shiwei.pet.mapper.PetDetailMapper;
import cn.shiwei.pet.mapper.PetMapper;
import cn.shiwei.pet.mapper.SearchMasterMsgMapper;
import cn.shiwei.pet.query.SearchMasterMsgQuery;
import cn.shiwei.pet.service.ISearchMasterMsgService;
import cn.shiwei.user.domain.Logininfo;
import cn.shiwei.user.domain.User;
import cn.shiwei.user.mapper.LogininfoMapper;
import cn.shiwei.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Logininfo logininfo) {
        // 判断登录信息否过期
        if (logininfo == null) {
            throw new MyException("登录过期, 请重新登录");
        }
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        searchMasterMsg.setUser(user);
        searchMasterMsg.setUser_id(user.getId());
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        List<Shop> shops = shopMapper.loadAll();
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setShop_id(nearestShop.getId());
        searchMasterMsgMapper.save(searchMasterMsg);
    }

    @Override
    public void handle(Pet pet, Long logininfo_id) {
        petMapper.save(pet);
        Long search_master_msg_id = pet.getSearch_master_msg_id();
        searchMasterMsgMapper.updateState(search_master_msg_id);
        PetDetail detail = pet.getDetail();
        detail.setPet_id(pet.getId());
        petDetailMapper.save(detail);
    }

    @Override
    public PageList<SearchMasterMsg> queryDate(SearchMasterMsgQuery query, Long logininfo_id) {
        Long shop_p = shopMapper.getShopIdByLogininfo_id(logininfo_id);
        if (shop_p != null) {
            query.setAdmin_type(shop_p);
        }

        Integer total = searchMasterMsgMapper.queryTotal(query);
        List<SearchMasterMsg> searchMasterMsgs = searchMasterMsgMapper.queryDate(query);
        PageList<SearchMasterMsg> pageList = new PageList<>(total ,searchMasterMsgs);
        return pageList;
    }
//    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}
